﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
using System.Xml;

namespace jihuang_Player
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class loadxml : Window
    {
        MainWindow main;
        string buildpath = "";
        string anpath = "";
        bool b = false;
        bool a = false;
        string flordname = "";
        int rep = 0;
        public loadxml(MainWindow wind)
        {
            InitializeComponent();
            buinput.IsEnabled = false;
            aninput.IsEnabled = false;
            main = wind;
        }

        private string GenerateCheckCodeNum(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "(build.xml)|*.xml";
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //Get the path of specified file
                    buildpath = openFileDialog.FileName;
                    buinput.Text = buildpath;
                    b = true;
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var fileContent = string.Empty;
            var filePath = string.Empty;
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "(build.xml)|*.xml";
                openFileDialog.RestoreDirectory = true;

                if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    //Get the path of specified file
                    anpath = openFileDialog.FileName;
                    aninput.Text = anpath;
                    a = true;
                }
            }
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            if (b)
            {
                if (a)
                {
                    string ancontent = "";
                    using (StreamReader reader = new StreamReader(anpath))
                    {
                        ancontent = reader.ReadToEnd();
                    }
                    XmlDocument anxml = new XmlDocument();
                    anxml.LoadXml(ancontent);
                    if (anxml.GetElementsByTagName("Anims")[0] == null)
                    {
                        System.Windows.MessageBox.Show("animation不正确！！！");
                        return;
                    }
                    FileInfo fileinfo = new FileInfo(buildpath);
                    string mainpath = fileinfo.Directory.FullName + @"\";
                    string fileContent;
                    using (StreamReader reader = new StreamReader(buildpath))
                    {
                        fileContent = reader.ReadToEnd();
                    }
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.LoadXml(fileContent);
                    XmlNode build = xmlDocument.GetElementsByTagName("Build")[0];
                    if (build == null)
                    {
                        System.Windows.MessageBox.Show("build不正确！！！");
                    }
                    else
                    {
                        flordname = build.Attributes["name"].Value.Split('.')[0] + "_" + GenerateCheckCodeNum(4);
                        Directory.CreateDirectory(@"Project\" + flordname);
                        XmlNodeList Symbols = build.SelectNodes("Symbol");
                        foreach (XmlNode Symbol in Symbols)
                        {
                            XmlNodeList Frames = Symbol.SelectNodes("Frame");
                            foreach (XmlNode Frame in Frames)
                            {
                                string imgname = Frame.Attributes["image"].Value;
                                string yuanimg = mainpath + imgname + ".png";
                                string targetimg = "Project\\" + flordname + "\\" + imgname + ".png";
                                if (File.Exists(yuanimg))
                                {
                                    File.Copy(yuanimg, targetimg, true);
                                }
                                else
                                {
                                    System.Windows.MessageBox.Show("找不到文件" + yuanimg);
                                }
                            }
                        }
                        xmlDocument.Save("Project\\" + flordname + "\\build.xml");
                        anxml.Save("Project\\" + flordname + "\\animation.xml");
                        CopyDirectory(MainWindow.rootPath + @"\resources\publicProjectSources\", MainWindow.rootPath + @"\Project\" + flordname);

                        main.notifyreflushProjetlist();
                        this.Close();
                    }
                }
                else
                {
                    System.Windows.MessageBox.Show("animation未选择");
                }
            }
            else
            {
                System.Windows.MessageBox.Show("build未选择");
            }
        }

        private void CopyDirectory(string sourceDirPath, string SaveDirPath)
        {
            try
            {
                //如果指定的存储路径不存在，则创建该存储路径
                if (!Directory.Exists(SaveDirPath))
                {
                    //创建
                    Directory.CreateDirectory(SaveDirPath);
                }
                //获取源路径文件的名称
                string[] files = Directory.GetFiles(sourceDirPath);
                //遍历子文件夹的所有文件
                foreach (string file in files)
                {
                    string pFilePath = SaveDirPath + "\\" + Path.GetFileName(file);
                    if (File.Exists(pFilePath))
                        continue;
                    File.Copy(file, pFilePath, true);
                }
                string[] dirs = Directory.GetDirectories(sourceDirPath);
                //递归，遍历文件夹
                foreach (string dir in dirs)
                {
                    CopyDirectory(dir, SaveDirPath + "\\" + Path.GetFileName(dir));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
